package voicecall.shb.echo;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

/**
 * Created by dyh on 2019/1/29.
 */
public class EchoServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
//        super.channelReadComplete(ctx);

        System.out.print("EchoServerHandle chanelReadComplete\n" );


    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//        super.channelRead(ctx, msg);
        ByteBuf data = (ByteBuf) msg;

        System.out.print("服务端收到消息：" + data.toString(CharsetUtil.UTF_8)+"\n");
//        System.out.print("服务端收到消息：" + data.toString());

        ctx.writeAndFlush(data);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
        ctx.close();
        System.out.print("EchoServerHandle exceptionCaught\n");
    }
}
